<?php

namespace services\patient;

use common\components\Service;
use common\models\patient\HistoryRecord;
use Yii;

class PatientHistoryRecordService extends Service
{
    /**
     * @param int $projectId
     * @param int $patientId
     * @param int $actionType
     * @param string $oldContent
     * @param string $newContent
     * @return void
     * @throws \Exception
     */
    public static function create(int $projectId, int $patientId, int $dataType, string $oldContent, string $newContent): void
    {
        if (empty($oldContent) || $newContent == $oldContent) {
            return;
        }

        $model = HistoryRecord::findOne([
            'project_id' => $projectId,
            'patient_user_id' => $patientId,
            'data_type' => $dataType,
            'content' => $oldContent,
        ]);
        if (!$model) {
            $model = new HistoryRecord();
            $model->project_id = $projectId;
            $model->patient_user_id = $patientId;
            $model->data_type = $dataType;
            $model->content = $oldContent;
        }
        $model->updated_at = time();
        $model->saveAndCheckResult();
    }
}
